#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "cJSON.h"

int generate_bin_file(cJSON *root, const char *filename){
	FILE *bin_file = fopen(filename, "wb");
	if (!bin_file) {
		perror("无法创建bin文件");
		return -1;
	}

	cJSON *item;
	cJSON_ArrayForEach(item, root){
		if (strcmp(item->string, "description")==0) continue;
		if (cJSON_IsArray(item)) {
			cJSON *val1 = cJSON_GetArrayItem(item, 0);
			cJSON *val2 = cJSON_GetArrayItem(item, 1);
			cJSON *val3 = cJSON_GetArrayItem(item, 2);
			if (!cJSON_IsString(val1) || !cJSON_IsString(val2) || !cJSON_IsString(val3)) {
				perror("无效数据格式");
				continue;
			}
			uint8_t bytes[3] = {
				(uint8_t)strtoul(val1->valuestring, NULL, 16),
				(uint8_t)strtoul(val2->valuestring, NULL, 16),
				(uint8_t)strtoul(val3->valuestring, NULL, 16)
			};

			if (fwrite(bytes, sizeof(uint8_t), 3, bin_file) !=3) {
				perror("写入文件失败");
				fclose(bin_file);
				return -1;
			}
		}
	}
	fclose(bin_file);
	return 0;
}

int main(int argc, char *argv[]) {
	if (argc < 2){
		return 1;
	}
	int mode = atoi(argv[1]);
	int slot_index = 0;
	if(mode > 0){
		slot_index = mode;
	}

	FILE *file = fopen("/mockdata/IMU/pcie_card.json", "r");
	if (!file) {
		perror("无法打开文件");
		return 1;
	}

	fseek(file, 0, SEEK_END);
	long size = ftell(file);
	fseek(file, 0, SEEK_SET);

	char * json_data = (char *)malloc(size+1);
	fread(json_data, 1, size, file);
	fclose(file);
	json_data[size] = '\0';

	cJSON *root = cJSON_Parse(json_data);
	if (!root) {
		printf("JSON解析错误");
		free(json_data);
		return 1;
	}

	if(slot_index == 0){
		generate_bin_file(root, "/mockdata/IMU/pcie_card.bin");
	}
	cJSON *item = NULL;
	int slot = -1;
	cJSON_ArrayForEach(item, root) {
		slot++;
	}
	if (slot_index ==0){
		printf("%d ", slot*5);
	}
	int current_slot = 0;
	cJSON_ArrayForEach(item, root) {
		const char *key=item->string;
		if(key && strcmp(key, "description") == 0) continue;
		current_slot++;
		if (cJSON_IsArray(item)) {
			if(slot_index >0 && current_slot ==slot_index){
				cJSON *bus = cJSON_GetArrayItem(item, 3);
				cJSON *device = cJSON_GetArrayItem(item, 4);
				cJSON *function = cJSON_GetArrayItem(item, 5);
				
				if (bus && device && function &&
					cJSON_IsString(bus) &&
					cJSON_IsString(device) &&
					cJSON_IsString(function)) {
						printf("%d %d %d\n", 
						strtol(bus->valuestring, NULL, 16),
						strtol(device->valuestring, NULL, 16),
						strtol(function->valuestring, NULL, 16));
				}
				break;
			}
			else if (slot_index == 0) {
				cJSON *presence = cJSON_GetArrayItem(item, 6);
				if (!presence || !cJSON_IsString(presence)) continue;
				if (strcmp(presence->valuestring, "1")==0){
					cJSON *devbus = cJSON_GetArrayItem(item, 0);
					cJSON *devdevice = cJSON_GetArrayItem(item, 1);
					cJSON *devfunction = cJSON_GetArrayItem(item, 2);

					if (devbus && devdevice && devfunction &&
						cJSON_IsString(devbus) &&
						cJSON_IsString(devdevice) &&
						cJSON_IsString(devfunction)) {
							printf("0 0 %d %d %d ", 
							strtol(devbus->valuestring, NULL, 16),
							strtol(devdevice->valuestring, NULL, 16),
							strtol(devfunction->valuestring, NULL, 16));
					}
				}
				else {
					printf("0 255 255 255 255 ");
				}
			}
		}
	}

	cJSON_Delete(root);
	free(json_data);
	return 0;
}